#!/usr/bin/python3
# -*- coding: utf-8 -*-
#======================================#
# @Author   : duanqizhong
# @time     : 2021-1-1 22:23
# @file     : ding.py
# @Software : PyCharm
#======================================#


import json
import requests

headers={"content-Type":"application/json;charset=UTF-8"}

# markdown text
def send_text(title,text,token,msgtype="text"):
    url=f"https://oapi.dingtalk.com/robot/send?access_token={token}"
    testMsg={
            "msgtype":msgtype,
            "text":{"content":f"{title}:\n{text}"}
            }
    try:
        send_msg=json.dumps(testMsg).encode("utf-8")
        requests.post(url=url,data=send_msg,headers=headers,timeout=5)
    except Exception as e:
        print(e)

def send_link(title,text,link,token):
    #token='f380f3b2791bec3dc584544703432aa3b4ce81af8b9bf82df936ca5ee10bf6ef'
    url="https://oapi.dingtalk.com/robot/send?access_token={token}".format(token=token)
    testMsg={
            "msgtype":"link",
            "link":{
                   "text":text,
                   "title":title,
                   "picUrl":"",
                   "messageUrl":link
                   }
            }
    try:
        send_msg=json.dumps(testMsg).encode("utf-8")
        requests.post(url=url,data=send_msg,headers=headers,timeout=5)
    except Exception as e:
        print(e)
